package com.logistics.distribution;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.logistics.database.Distributiondatabase;

public class Storeroommanageservlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Storeroommanageservlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		response.setContentType("text/xml;charset=UTF-8");
	    response.setHeader("Cache-Control", "no-cache");
		//System.out.println(action);
		if(action.equals("refresh"))
		{
			//System.out.println("!!!");
			String product_name = request.getParameter("product_name");
			if(product_name==null)
				product_name="";
			String product_id = request.getParameter("product_id");
			if(product_id==null)
				product_id="";
			ArrayList<Product> products = new ArrayList<Product>();
			Distributiondatabase distributiondatabase = new Distributiondatabase();
			ResultSet resultSet = distributiondatabase.productsQuery();
			try {
				while(resultSet.next())
				{
					Product tempproduct = new Product();
					tempproduct.setProduct_id(String.valueOf(resultSet.getInt("product_id")));
					tempproduct.setProduct_name(resultSet.getString("product_name"));
					tempproduct.setWarning_number(String.valueOf(resultSet.getInt("warning_number")));
					tempproduct.setMax_number(String.valueOf(resultSet.getInt("max_number")));
					products.add(tempproduct);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(!product_id.equals(""))
			{
				for(int i = products.size()-1;i>=0;--i)
				{
					if(!products.get(i).getProduct_id().equals(product_id))
					{
						products.remove(i);
					}
				}
			}
			if(!product_name.equals(""))
			{
				for(int i = products.size()-1;i>=0;--i)
				{
					if(!products.get(i).getProduct_name().equals(product_name))
					{
						products.remove(i);
					}
				}
			}
			String xmlstart = "<products>";
			String xmlend = "</products>";
			String xml = "";
			for(int i = 0; i<products.size();++i)
			{
				xml+="<product><product_id>"+products.get(i).getProduct_id()+"</product_id><product_name>"+products.get(i).getProduct_name()+"</product_name><warning_number>"+products.get(i).getWarning_number()+"</warning_number><max_number>"+products.get(i).getMax_number()+"</max_number></product>";
			}
			String xml_last = xmlstart+xml+xmlend;
			response.getWriter().write(xml_last);
	    	response.getWriter().close();
			//System.out.println(xml_last);
		}
		else if(action.equals("mod"))
		{
			String mod_id = request.getParameter("product_id");
			String mod_warning_number = request.getParameter("warning_number");
			String mod_max_number = request.getParameter("max_number");
			System.out.println(mod_id);
			System.out.println(mod_warning_number);
			System.out.println(mod_max_number);
			Distributiondatabase distributiondatabase = new Distributiondatabase();
			distributiondatabase.updateProductwithstoreroom(mod_id, mod_warning_number, mod_max_number);
			response.getWriter().write("123");
	    	response.getWriter().close();
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
